import 'dart:math';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

typedef controllerCallBack = Future<void> Function(AnimationController controller,Animation dy);

class AnimationLG extends StatefulWidget{
  @required final controllerCallBack onTap;
  @required final Widget childR;
  @required final Widget child;
  final Duration duration;
  AnimationLG({Key key,this.onTap,this.child,this.childR,this.duration}):
        //assert(onTap != null),
        assert(child != null),
        super(key:key);
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _AnimateionLGState();
  }


}
class _AnimateionLGState extends State<AnimationLG> with SingleTickerProviderStateMixin{
  AnimationController _controller;
  Animation<double> _animation;
  Tween _tween;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _controller = AnimationController(duration: widget.duration == null ? Duration(seconds: 5): widget.duration,vsync: this);
    _tween = Tween<double>(begin: 0,end: 1);
    _animation = _tween.animate(_controller);
  }
  @override
  void dispose() {
    // TODO: implement dispose
    _controller.dispose();
    super.dispose();

  }
  bool isforward = false;
  @override
  void didUpdateWidget(AnimationLG oldWidget) {
    // TODO: implement didUpdateWidget
    super.didUpdateWidget(oldWidget);
    //_controller = null;
    //_controller = AnimationController(duration: widget.duration == null ? Duration(seconds: 5): widget.duration,vsync: this);

  }
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return InkWell(
      child: MyAnimatedWidget(animation: _animation,childR: widget.childR,child: widget.child,),
      onTap: (){
        if(widget.onTap == null){
          return;
        }
        widget.onTap(_controller,_animation);
      },
    );
  }

}


class MyAnimatedWidget extends AnimatedWidget{
  @required final Animation<double> animation;
  @required final Widget child;
  @required final Widget childR;
  MyAnimatedWidget({Key key,this.animation,this.child,this.childR,}):super(key:key,listenable:animation);
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    // return Transform.rotate(
    //
    //   origin: Offset(1,1),
    //   angle: 10.0,
    //   //angle: animation.value,
    //   child: Container(
    //     width: 80.0,
    //     height: 100.0,
    //     alignment: Alignment.center,
    //     decoration: BoxDecoration(
    //         border: Border.all(),
    //         color: Colors.blueAccent.withOpacity(0.4)
    //     ),
    //     //transform: Matrix4.rotationY(animation.value),
    //
    //     child: Text('${(animation.value * 100).toStringAsFixed(2) }'),
    //   ),
    // );
    return  Stack(
      //alignment: Alignment.center,
      children: <Widget>[
        // AnimatedOpacity(
        //   duration: Duration(seconds: 5),
        //   child: Container(
        //     width: 80.0,
        //     height: 100.0,
        //     decoration: BoxDecoration(
        //       border: Border.all(),
        //       color: Colors.blueAccent
        //     ),
        //   ),
        //   opacity: 1 - animation.value,
        // ),
        Container(
          width: double.infinity,
          height: double.infinity,
          child: Opacity(
            child: child,
            opacity: 1 - animation.value,
          ),
        ),

        Container(
          width: double.infinity,
          height: double.infinity,
          child: Transform(
              transform: Matrix4.identity()..setEntry(3, 2, 0.006)..rotateY(pi * animation.value),
              alignment: Alignment.center,
              child:Opacity(
                child: childR,
                opacity: animation.value,
              )
          ),
        )
      ],
    );
    return Transform(
      transform: Matrix4.identity()..setEntry(3, 2, 0.006)..rotateY(pi * animation.value),
      alignment: Alignment.center,
      child:Container(
        width: 80.0,
        height: 100.0,
        color: Colors.blueAccent.withOpacity(0.5),
      )
    );

  }
  _childO(){
    return ClipRect(
      child: Align(
        alignment: Alignment.center,
        widthFactor: 1,
        child: Container(
          width: 80.0,
          height: 100.0,
          color: Colors.blueAccent.withOpacity(0.5),
        ),
      ),
    );
  }
}